 /***************************************************************************************/
// Author                 - Balaji Venkatesan
// Student ID             - 12527079
// Version                - 1.0
// CodeName               - Northodyne Solutions
// Module                 - E-Business Technologies
// Date                   - 12-September-2011
/***************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class CustomerRegistration : System.Web.UI.Page
{
    public string _connStr = "Initial Catalog=DBNorthodyneSolutions; Data Source=ABC\\SQLExpress;Trusted_Connection=Yes"; 

    public bool ddlSexFlag = false;

    //public string customerPrivilegeID = "123";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!ddlSexFlag)
        {
            ddlSex.Items.Add("Male");
            ddlSex.Items.Add("Female");
            ddlSexFlag = true;
        }

        Label lblwelcome = (Label)Master.FindControl("lblWelcome");
        Label lblusername = (Label)Master.FindControl("lblUserName");
        Label lblexpression = (Label)Master.FindControl("lblExpression");
        HyperLink hyplogin = (HyperLink)Master.FindControl("hyplogin");
        lblwelcome.Visible = false;
        lblusername.Visible = false;
        lblexpression.Visible = false;
        hyplogin.Visible = false;
    }

    protected void btnCustomerRegister_Click(object sender, EventArgs e)
    {
        int returnValue = 0;

        using (SqlConnection connection = new SqlConnection(_connStr))
        {
            connection.Open();

            string insertCustomerInfo = "INSERT INTO Tb_Customers (EMailID, FirstName, LastName,UserName,Sex,Address,ContactNumber) VALUES " + "(@EMailID,@FirstName,@LastName,@UserName, @Sex,@Address,@ContactNumber)";

            using (SqlCommand insertCustomerCommand = new SqlCommand(insertCustomerInfo, connection))
            {
                SqlParameter[] prms = new SqlParameter[7];
                prms[0] = new SqlParameter("@EMailID", SqlDbType.NVarChar, 50);
                prms[0].SqlValue = txtEmailID.Text.Trim();
                prms[1] = new SqlParameter("@FirstName", SqlDbType.NVarChar, 50);
                prms[1].SqlValue = txtFirstName.Text.Trim();
                prms[2] = new SqlParameter("@LastName", SqlDbType.NVarChar, 50);
                prms[2].SqlValue = txtLastName.Text.Trim(); //int.Parse(txtAge.Text.Trim());
                prms[3] = new SqlParameter("@UserName", SqlDbType.NVarChar, 50);
                prms[3].SqlValue = txtUserName.Text.Trim(); //bool.Parse(dropActive.SelectedValue);
                prms[4] = new SqlParameter("@Sex", SqlDbType.NVarChar, 50);
                prms[4].SqlValue = ddlSex.SelectedItem.Text.Trim();
                prms[5] = new SqlParameter("@Address", SqlDbType.NVarChar, 50);
                prms[5].SqlValue = txtAddress.Text.Trim();
                prms[6] = new SqlParameter("@ContactNumber", SqlDbType.NVarChar, 50);
                prms[6].SqlValue = txtContactNumber.Text.Trim();

                insertCustomerCommand.Parameters.AddRange(prms);
                returnValue = insertCustomerCommand.ExecuteNonQuery();
            }


            string insertLoginCredentials = "INSERT INTO Tb_Login (UserName, Password, UserType, SecretQuestion, SecretAnswer) VALUES " + "(@UserName,@Password,@UserType,@SecretQuestion,@SecretAnswer)";

            using (SqlCommand insertLoginCommand = new SqlCommand(insertLoginCredentials, connection))
            {
                SqlParameter[] prms = new SqlParameter[5];
                prms[0] = new SqlParameter("@UserName", SqlDbType.NVarChar, 50);
                prms[0].SqlValue = txtUserName.Text.Trim();
                prms[1] = new SqlParameter("@Password", SqlDbType.NVarChar, 50);
                prms[1].SqlValue = txtPassword.Text.Trim();
                prms[2] = new SqlParameter("@UserType", SqlDbType.NVarChar, 50);
                prms[2].SqlValue = "customer"; //int.Parse(txtAge.Text.Trim());
                prms[3] = new SqlParameter("@SecretQuestion", SqlDbType.NVarChar, 50);
                prms[3].SqlValue = txtSecretQuestion.Text.Trim(); //bool.Parse(dropActive.SelectedValue);
                prms[4] = new SqlParameter("@SecretAnswer", SqlDbType.NVarChar, 50);
                prms[4].SqlValue = txtSecretAnswer.Text.Trim();

                insertLoginCommand.Parameters.AddRange(prms);
                returnValue = insertLoginCommand.ExecuteNonQuery();
            }

            connection.Close();
        }

        Response.Redirect("Home.aspx");

    }
    protected void btnHome_Click(object sender, EventArgs e)
    {
        Response.Redirect("Home.aspx");
    }
}

#region Comments

//using (SqlConnection conn = new SqlConnection(_connStr))
//{
// write the sql statement to execute

/* UPDATE COMMAND */

//string updatesql = "UPDATE Tb_Customers SET UserName = @UserName WHERE EMailID = @EMailID";
//using (SqlCommand updatecmd = new SqlCommand(updatesql, conn))
//{
//    SqlParameter[] prms = new SqlParameter[2];
//    prms[0] = new SqlParameter("@UserName", SqlDbType.NVarChar, 50);
//    prms[0].SqlValue = "alan_s";
//    prms[1] = new SqlParameter("@EMailID", SqlDbType.NVarChar, 50);
//    prms[1].SqlValue = "alan_s@yahoo.com";

//    updatecmd.Parameters.AddRange(prms);
//    conn.Open();
//    returnValue = updatecmd.ExecuteNonQuery();
//    conn.Close();
//}


/* SELECT COMMAND */


//string selectsql = "SELECT * from Tb_Customers WHERE LastName = @Lastname";
//using (SqlCommand selectcmd = new SqlCommand(selectsql, conn))
//{
//    SqlParameter[] prms = new SqlParameter[1];
//    prms[0] = new SqlParameter("@Lastname", SqlDbType.NVarChar, 50);
//    prms[0].SqlValue = "siow";
//    object[] customervalues = new object[100];

//    selectcmd.Parameters.AddRange(prms);
//    conn.Open();

//    using (SqlDataReader reader = selectcmd.ExecuteReader())
//    {
//        while (reader.Read())
//        {
//            Response.Write(reader.GetString(0));
//            Response.Write(reader.GetString(3));
//            Response.Write(reader.GetString(6));
//        }
//    }

//    //returnValue = selectcmd.ExecuteNonQuery();
//    conn.Close();
//}


/* INSERT COMMAND */

// instantiate the command object to fire
//string sql = "INSERT INTO Tb_Customers (EMailID, FirstName, LastName,UserName,Sex,PrivilegeID,Address,ContactNumber) VALUES " + "(@EMailID,@FirstName,@LastName,@UserName, @Sex,@PrivilegeID,@Address,@ContactNumber)";
//using (SqlCommand cmd = new SqlCommand(sql, conn))
//{
//    // attach the parameter to pass, if no parameter is in the sql no need to attach
//    SqlParameter[] prms = new SqlParameter[8];
//    prms[0] = new SqlParameter("@EMailID", SqlDbType.NVarChar, 50);
//    prms[0].SqlValue = txtEmailID.Text.Trim();
//    prms[1] = new SqlParameter("@FirstName", SqlDbType.NVarChar, 50);
//    prms[1].SqlValue = txtFirstName.Text.Trim();
//    prms[2] = new SqlParameter("@LastName", SqlDbType.NVarChar,50);
//    prms[2].SqlValue = txtLastName.Text.Trim(); //int.Parse(txtAge.Text.Trim());
//    prms[3] = new SqlParameter("@UserName", SqlDbType.NVarChar,50);
//    prms[3].SqlValue = txtUserName.Text.Trim(); //bool.Parse(dropActive.SelectedValue);
//    prms[4] = new SqlParameter("@Sex", SqlDbType.NVarChar, 50);
//    prms[4].SqlValue = ddlSex.SelectedItem.Text.Trim();
//    prms[5] = new SqlParameter("@PrivilegeID", SqlDbType.NVarChar, 20);
//    prms[5].SqlValue = privilegeID;
//    prms[6] = new SqlParameter("@Address", SqlDbType.NVarChar, 50);
//    prms[6].SqlValue = txtAddress.Text.Trim();
//    prms[7] = new SqlParameter("@ContactNumber", SqlDbType.NVarChar, 50);
//    prms[7].SqlValue = txtContactNumber.Text.Trim();

//    cmd.Parameters.AddRange(prms);
//    conn.Open();
//    returnValue= cmd.ExecuteNonQuery();
//    conn.Close();
//}
//}

#endregion